package twi;

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;

import twi.query.Query;
import twi.result.DefaultResult;
import twi.result.QueryResult;


public class QueryManager {

	
	public static QueryResult executeQuery(Query query){
		ResultSet rs = executeSparqlQuery(query);
		DefaultResult result = new DefaultResult(query, rs);
		return result;
	}
	
	private static ResultSet executeSparqlQuery(Query query) {
		OWLModel model = query.getModel();
		if (model == null)
			throw new NullPointerException("OWLModel for query may not be null");
		
		com.hp.hpl.jena.query.Query q = QueryFactory.create(query.getSPARQLString());
		QueryExecution qexec = QueryExecutionFactory.create(q, model);
		ResultSet rs = qexec.execSelect();
		return rs;

	}
}
